
<template>
  <el-dialog
    title="新增图源信息"
    :visible.sync="dialogVisible"
    width="26%"
    :before-close="close"
  >
    <!-- <el-row>
          <el-col :span="24"
            ><div class="grid-content bg-purple-dark">11111</div
          ></el-col>
        </el-row> -->
    <el-form
      :inline="true"
      :model="imageFrom"
      label-position="right"
      :rules="rules"
      ref="ruleForm"
    >
      <el-form-item label="图源ID：" label-width="100px" prop="symbolId">
        <el-input
          v-model="imageFrom.symbolId"
          placeholder="请输入图源ID"
          clearable
          :style="{ width: $px2rem('240px') }"
        ></el-input>
      </el-form-item>
      <el-form-item label="图源类型：" label-width="100px">
        <el-input
          v-model="imageFrom.DeviceSubtype"
          placeholder="请输入图源类型"
          clearable
          :style="{ width: $px2rem('240px') }"
        ></el-input>
      </el-form-item>
      <el-form-item label="图源子类型：" label-width="100px">
        <el-input
          v-model="imageFrom.DeviceType"
          placeholder="请输入图源子类型"
          :style="{ width: $px2rem('240px') }"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="图源名称：" label-width="100px" prop="imageRemark">
        <el-input
          v-model="imageFrom.imageRemark"
          placeholder="请输入图源名称"
          :style="{ width: $px2rem('240px') }"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="上传图片：" label-width="100px" prop="file">
        <input
          type="file"
          @change="changeimg"
          ref="imgfile"
          accept="image/png, image/jpeg, image/jpg"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="canceaddllation">取 消</el-button>
      <el-button type="primary" @click="deteraddmine">提交</el-button>
    </span>
  </el-dialog>
</template>
    <script>
import { AddImageSource } from "../../../api/index";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入"));
      } else {
        callback();
      }
    };
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请上传图片"));
      } else {
        callback();
      }
    };
    return {
      imageFrom: {
        symbolId: "",
        DeviceSubtype: "",
        DeviceType: "",
        file: "",
        imageRemark: "",
        source: "mapbox",
      },
      rules: {
        symbolId: [{ validator: validatePass, trigger: "blur" }],
        imageRemark: [{ validator: validatePass, trigger: "blur" }],
        file: [{ validator: validatePass1, trigger: "blur" }],
      },
      //   dialogVisible: false,
    };
  },
  methods: {
    empty(obj) {
      for (let i in obj) {
        obj[i] = "";
      }
      this.$refs["ruleForm"].resetFields();
      this.$refs.imgfile.value = "";
    },
    close() {
      this.empty(this.imageFrom);
      this.$emit("canceadd", false);
    },
    canceaddllation() {
      this.empty(this.imageFrom);
      this.$emit("canceadd", false);
    },
    deteraddmine() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          try {
            let res = await AddImageSource(this.imageFrom);
            this.$message.success(`${res.message}`);
          } catch (err) {
            this.$message.error(`${err}`);
          } finally {
            this.empty(this.imageFrom);
            this.$emit("deteradd", false);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    changeimg(e) {
      //获取上传文件对象 赋值
      this.imageFrom.file = e.srcElement.files[0];
    },
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: () => false,
    },
  },
  mounted() {},
};
</script>
    <style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px !important;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>